﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JuCheap.Core.Infrastructure.Extentions;
using JuCheap.Core.Interfaces;
using JuCheap.Core.Models.Filters;
using JuCheap.Core.Models.ViewModels;
using JuCheap.Core.Web.Filters;
using JuCheap.Core.Web.Models;
using Microsoft.AspNetCore.Mvc;

namespace JuCheap.Core.Web.Controllers
{
    public class TimePeriodController : Controller
    {
        private IRepository _iRepository;
        public TimePeriodController(IRepository iRepository)
        {
            _iRepository = iRepository;
        }
        public IActionResult Index()
        {
            return View();
        }
        public async Task<IActionResult> Delete([FromBody]List<string> ids)
        {
            var result = new JsonResultModel<bool>();
            if (ids.AnyOne())
            {
                result.flag = true;
                foreach (string str in ids)
                {
                    await _iRepository.DeleteTimePeriod(Convert.ToInt32(str));
                }
            }
            return Json(result);
        }
        public IActionResult Add()
        {

            return View(new TimePeriodViewModel());
        }
        
        public async Task<IActionResult> Edit(int id)
        {
            var dto = await _iRepository.GetTimePeriods(new TimePeriodFilter() { id = id, });

            if (dto == null || dto.rows.Count == 0)
            {
                return RedirectToAction("Add");
            }
            return View(dto.rows[0]);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(TimePeriodViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await _iRepository.UpdateTimePeriod(model);
                return RedirectToAction("Index");
            }
            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Add(TimePeriodViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await _iRepository.AddTimePeriod(model);
                //if (result.IsNotBlank())
                return RedirectToAction("Index");
            }
            return View(model);
        }

        [HttpGet, IgnoreRightFilter]
        public async Task<ActionResult> GetTimePeriod(string q)
        {
            var filter = new TimePeriodFilter
            {
                page = 1,
                rows = 1000,
                keywords = null
            };
            var result = await _iRepository.GetTimePeriods(filter);
            var depts = new
            {
                results = result.rows.Select(x => new { id = x.Id, text = x.PeriodName })
            };
            return Json(depts);                                          
        }


        [IgnoreRightFilter]
        public async Task<IActionResult> GetListWithPager(TimePeriodFilter filters)
        {
            var result = await _iRepository.GetTimePeriods(filters);
            foreach(var r in result.rows)
            {
                if (r.TimeTypePeriod == 1)
                    r.timeType = "折算工作小时";
                else
                    r.timeType = "折算天数";
            }
            return Json(result);
        }
    }
}